<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-06-25 10:11
 * Modified Time: 2019-06-25 10:11
 * Version: 2.0
 */

namespace App\Modules\Employee\Repositories;

use App\Exceptions\NotFoundException;
use App\Lib\Utilities;
use App\Models\Dynamic;
use App\Modules\Employee\Criterias\GetDynamicListCriteria;
use App\Modules\Employee\Validators\DynamicValidator;
use App\Modules\Order\Criterias\getDynamicResourceCriteria;
use Dotenv\Exception\ValidationException;
use Flower\Core\Repository\Eloquent\BaseRepository;
use Flower\Core\Repository\Exceptions\ValidatorException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\MessageBag;

class DynamicRepository extends BaseRepository
{
	public function model()
	{
		return Dynamic::class;
	}

	public function validator()
	{
		return DynamicValidator::class;
	}


	/**
	 * 获取动态列表
	 * @param $userIds
	 * @return mixed
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 */
	public function getList($userIds,$store_id,$pageSize){

		$pageSize = empty($pageSize) ? 10 : $pageSize;
		return $this->pushCriteria(new GetDynamicListCriteria($userIds,$store_id))->paginate($pageSize, ['*'], 'paginate');
//		return $this->model->with('')
//		$limit = isset($requestData['limit']) ? $requestData['limit'] : 5;
//		return $this->model->selectRaw('*')->where($requestData)->with(['resource'=>function($query){
//			return $query->selectRaw('*')->when('type=1',function($query){
//				return $query->with(['picture'=>function($query){
//                            return $query->selectRaw('*');
//                        }])->when('type=1',function($query){
//                                return $query->with(['video'=>function($query){
//                                    return $query->selectRaw('*');
//                                }]);
//				});
//			});
//		}])->limit($limit)->get()->toArray();
	}


	/**
	 * 修改动态
	 * @param $dynamicId
	 * @param $detail
	 * @return mixed
	 */
	public function edit($dynamicId,$detail){
		return $this->update(['detail'=>$detail],$dynamicId);
	}



	/**
	 * 动态检测
	 * @param $ids
	 * @return mixed
	 * @throws NotFoundException
	 */
	public function isExisted($ids){
		$dynamic = $this->find($ids);
		if(empty($dynamic)){
			throw new NotFoundException(['msg'=>"该动态不存在！"]);
		}
		return $dynamic;
	}

	/**
	 * 计算店铺或用户的某天动态数
	 * @param $usersId
	 * @param string $date
	 * @return int
	 */
	public function count($usersId,$date =''){
		$res = 0;
		$date = empty($date) ? date('Y-m-d', time()): $date;
		try{
			$res = $this->model->whereIn('user_id',$usersId)->whereDate('created_at', $date)->count();
			return $res;
		}catch (ValidationException $e){
			return $res;
		}
	}

	/**
	 * 新增动态
	 * @param $userId
	 * @param $detail
	 * @return mixed
	 * @author Happy dong
	 * @modifiedTime 2019-08-09 14:50
	 */
	public function addDynamic($userId,$store_id,$detail){
		$params = [
            'user_id' =>$userId,
            'store_id' =>$store_id,
			'detail'=>$detail,
		];
		return $this->create($params);
	}
}